//
//  DCBeautyShopRecommendCell.m
//  商城
//
//  Created by runone02 on 2018/8/29.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "DCBeautyShopRecommendCell.h"
#import "DCStoresRecommendCell.h"
#import "DCStoresRecommendHeaderView.h"
#import "DCPageControl.h"
@interface DCBeautyShopRecommendCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
/* 顶部标题View */
@property (strong, nonatomic) DCStoresRecommendHeaderView *topTitleView;
/* 底部上架View */
@property (strong, nonatomic) UIView *bottomPutawayView;
/* 页面 */
@property (strong, nonatomic) DCPageControl *pageControl;

/* collectionView */
@property (strong, nonatomic) UICollectionView *collectionView;
/** 点*/
@property (nonatomic,strong) UIView *lineView;

@end

static NSString *const DCStoresRecommendCellID = @"DCStoresRecommendCell";

@implementation DCBeautyShopRecommendCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setUpAllChildView];
    }
    return self;
}

- (void)setUpAllChildView{
    WEAKSELF
    self.topTitleView = [[DCStoresRecommendHeaderView alloc]init];
    self.topTitleView.headRecommendLabel.text = @"商品推荐";
    self.topTitleView.moreLabel.text = @"更多>";
    [self addSubview:self.topTitleView];
    [self.topTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
        make.height.mas_equalTo(35);
    }];
    
    self.bottomPutawayView = [[UIView alloc]init];
    self.bottomPutawayView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bottomPutawayView];
    [self.bottomPutawayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(50);
    }];
    
    UIButton *allButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [allButton setTitle:@"上架" forState:UIControlStateNormal];
    allButton.backgroundColor = [UIColor redColor];
    allButton.titleLabel.font = TITLE_FONT(30);
    [allButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [allButton.layer setMasksToBounds:YES];
    [allButton.layer setCornerRadius:10.0];
    
    [allButton.layer setBorderWidth:1.0];
    allButton.layer.borderColor = [UIColor redColor].CGColor;
    

    [self.bottomPutawayView addSubview:allButton];
    [allButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(weakSelf.bottomPutawayView);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.bottomPutawayView.mas_top);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(40);
    }];
    
    UICollectionViewFlowLayout *dcFlowLayout = [UICollectionViewFlowLayout new];
    dcFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    dcFlowLayout.minimumLineSpacing = dcFlowLayout.minimumInteritemSpacing = 0;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:dcFlowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[DCStoresRecommendCell class] forCellWithReuseIdentifier:DCStoresRecommendCellID];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.topTitleView.mas_bottom).with.offset(10);
        make.left.mas_equalTo(self).with.offset(5);
        make.right.mas_equalTo(self).with.offset(-5);
       make.bottom.mas_equalTo(weakSelf.lineView.mas_top).with.offset(-10);
    }];
    
    self.pageControl = [[DCPageControl alloc]initWithFrame:CGRectZero];
    self.pageControl.userInteractionEnabled = NO;
    self.pageControl.hidesForSinglePage = YES;
    self.pageControl.numberOfPages = 4;
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor darkGrayColor];
    [_lineView addSubview:self.pageControl];
    
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.lineView);
        make.centerX.mas_equalTo(weakSelf.lineView);
        make.left.mas_equalTo(weakSelf.lineView);
        make.right.mas_equalTo(weakSelf.lineView);
        make.height.mas_equalTo(40);
    }];
}





- (void)layoutSubviews{
    [super layoutSubviews];
   
}


#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _beaShopItem.count;
}

- (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    DCStoresRecommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:DCStoresRecommendCellID forIndexPath:indexPath];
    cell.shopItem = _beaShopItem[indexPath.row];
    
    return cell;
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.pageControl.currentPage = scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame);
}


#pragma mark - <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了第%zd个推荐的商品",indexPath.row);
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(_collectionView.dc_width / 3, _collectionView.dc_height);
}

@end
